package com.splider.toutiao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

public class TouTiaoSplider {
	
	private static String[] categorys = { "__all__", "news_hot", "news_society", "news_entertainment", "news_tech",
			"news_sports", "news_car", "news_finance", "funny", "news_military", "news_world", "news_fashion",
			"news_travel", "news_discovery", "news_baby", "news_regimen", "news_story", "news_essay", "news_game",
			"news_history", "news_food" };
	
	private static Logger logger = Logger.getLogger("toutiao");
	
	private static ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());
	
	public static void main(String[] args) {
		int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
		int resultdata[] = new int[categorys.length];
		List<Integer> dataArray = new ArrayList<Integer>();
		for (int i = 0; i < data.length; i++) {
			dataArray.add(data[i]);
		}
		int i = 0;
		while (dataArray.size() > 0) {
			int index = (int) (Math.random() * dataArray.size());
			resultdata[i] = dataArray.get(index);
			i++;
			dataArray.remove(index);
		}
		logger.info("抓取代理ip");
		List<ProxyIP> proxyIps = CommonUtils.getProxyIPList();
		Collections.reverse(proxyIps);
		logger.info("抓取代理ip结束，总数:" + proxyIps.size());
		for (int j = 0; j < resultdata.length; j++) {
			executor.execute(new TouTiaoTask(categorys[resultdata[j]], proxyIps));
		}
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
		}
		executor.shutdown();
	}
}
